<template>
  <el-button slot="reference" @click="handleclick" :loading="loading" icon="el-icon-download">{{name}}</el-button>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'ExportList2Excel',
  data(){
      return{
         loading:false,
      }
  },
  props: {
      name:{
          type:String,
          default:"导出数据"
      }
  },
  computed: {
  },
  methods: {
    unlockLoading(){  //解锁按键
        this.loading = false
    },
    handleclick(){
        this.loading = true
        this.$emit('handleExport')
      setTimeout(()=>{
        this.loading = false
      },300)
    },
    handleDownload(tableData) {
        //传进来的数据tableData{tHeader:表头字段,
         //                     filterVal:表头字段对应的英文，顺序一定要和tHeader对应,
        //                     list:导出表格数据的正文,
        //                     filename:导出的文件名}
      //                       multiHeader:二维数组，与theader的length相等
      //                       merges:合并单元格，例如 ["A1:B2"]
        let _this = this
        // console.info("传过来的数据tableData--"+JSON.stringify(tableData))
        // console.info("传过来的数据tableData.tHeader--"+JSON.stringify(tableData.tHeader))
        // console.info("传过来的数据tableData.filterVal--"+JSON.stringify(tableData.filterVal))
        // console.info("传过来的数据tableData.list--"+JSON.stringify(tableData.list))
      console.log(tableData)
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = tableData.tHeader
          const filterVal = tableData.filterVal
          // debugger
          const data = this.formatJson(filterVal, tableData.list)
          excel.export_json_to_excel({
            multiHeader:tableData.multiHeader||[],
            header: tHeader,
            data:data,
            filename: tableData.filename,
            autoWidth: this.autoWidth,
            bookType: this.bookType,
            merges:tableData.merges||[],
          })
          _this.unlockLoading()
        }).catch(() => { _this.unlockLoading() })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]));
      },
}
}
</script>

<style scoped>
.pagination-container {
  background: #fff;
  /*padding: 32px 16px;*/
  margin-top: 10px !important;
}
.pagination-container.hidden {
  display: none;
}
</style>
